<template>
  <div>
    <div class="demo-drawer__content">
      <!-- 左侧显示 -->
      <div class="leftclass">
        <div style="background: #f5f7fa; height: 43px; line-height: 43px; text-align: center; font-size: 14px; color: #388fe8">
		  		{{
		  			this.workLineInfo.workDate == undefined
		  				? ""
		  				: this.workLineInfo.workDate.substring(0,10)+
		  				" " +
		  				(this.workLineInfo.workTime=="01"?'白班':'夜班') +
		  				" " +
		  				this.workLineInfo.processName
		  		}}
		  	</div>
          <div style="margin-left: 20px">
		  			<el-row>
		  				<el-col>
		  					开工：
		  					<span class="nomal">
		  						{{ this.workLineInfo.startWorkTime == undefined ? "" : this.  workLineInstartWorkTime }}
		  					</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					收工：
		  					<span class="nomal">
		  						{{ this.workLineInfo.endWorkTime == undefined ? "" : this.workLineInendWorkTime }}
		  					</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					泊位：
		  					<span class="nomal">{{ this.workLineInfo.berthNo }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					船名：
		  					<span class="nomal">{{ this.workLineInfo.shipName }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					舱口：
		  					<span class="nomal">{{ this.workLineInfo.cabinNo }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					货物：
		  					<span class="nomal">{{ this.workLineInfo.cargoCategoryName }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					队别：
		  					<span class="nomal">{{ this.workLineInfo.deptName }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					前沿：
		  					<span class="nomal">{{ this.workLineInfo.tallyFront }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					转运：
		  					<span class="nomal">{{ this.workLineInfo.transport }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					辅助：
		  					<span class="nomal">{{ this.workLineInfo.supporting }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					后沿：
		  					<span class="nomal">{{ this.workLineInfo.tallyBack }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					辅助：
		  					<span class="nomal">{{ this.workLineInfo.locationMachine_04 }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					理货员：
		  					<span class="nomal">{{ this.workLineInfo.tallyClerk }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					指导员：
		  					<span class="nomal">{{ this.workLineInfo.instructorName }}</span>
		  				</el-col>
		  			</el-row>
		  			<el-row>
		  				<el-col>
		  					作业要求：
		  					<span class="nomal">{{ this.workLineInfo.theRemark }}</span>
		  				</el-col>
		  			</el-row>
		  		</div>
      </div>
      <!-- 右侧显示 -->
      <div class="rightclass">
        <el-collapse v-model="activeName" @change="collapseChanges($event)" accordion>
          <el-collapse-item :name="index" v-for="(item,index) in cargoInfoList" :key="index">
            <template slot="title">
              <span>{{ item.cargoOwnerName }}/</span>
              <span>{{ item.cargoAgentName }}/</span>
              <span>{{ item.cargoName }}/</span>
              <span>{{ item.packingName }}/</span>
              <span>{{ item.tradeType }}/</span>
              <span>{{ item.nameVoyage }}</span>
            </template>
            <div class="search_form" style="border: 1px solid #d7dadb">
							<div style="margin-left: 12px">
								<el-radio-group v-model="ticketId" size="mini" @change="changeTiketInfo">
										<el-radio-button v-for="val in item.tallyList" :label="val.id" :key="val.id">
											{{ val.processName }}
										</el-radio-button>
								</el-radio-group>
              </div>
              <div>
                <el-button type="primary" size="mini" @click="addTicket">新增</el-button>
                <el-button size="mini" @click="deleteTicket" v-hasPermi="['yardManagement:TYardTicket:deleteById']">删除</el-button>
              </div>
            </div>
            <div class="subclass">
              <div>
							  <el-select
							  	style="margin-left: 10px"
							  	size="mini"
							  	filterable
							  	clearable
							  	placeholder="选择子过程"
							  	v-model="subProcessCode"
							  	@change="changeSourceDestination"
                  :disabled="processDisable"
							  >
							    <el-option
							  	  v-for="(item,index) in processList"
							  	  :key="index"
							  	  :label="item.processName"
							  	  :value="item.processCode"
							    />
						    </el-select>
              </div>
              <div class="buttonclass">
                <el-button type="text" size="mini" @click="useTallyData">使用理货数据</el-button>
                <el-button type="text" size="mini">使用过磅数据</el-button>
              </div>
					  </div>
            <!-- <el-form label-position="top" :rules="ruleForm" ref="ticketInfo" style="padding:10px 10px;" :model="ticketForm">
               从区域 --
              <div v-if="source == '1'">
                <el-row>
                  <li>船舶信息</li>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="船舶航次" prop="from.nameVoyage">
                      <el-input v-model="ticketForm.from.nameVoyage" size="mini" :disabled="isShowAmount" readonly></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="泊位" prop="berthNo">
                      <el-input v-model="ticketForm.from.berthNo" size="mini" :disabled="isShowAmount" readonly></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="舱口" prop="from.cabinNo">
                      <el-select v-model="ticketForm.from.cabinNo" :disabled="isShowAmount" style="width:100%;" size="mini" multiple>
                        <el-option 
                          v-for="item in cabinNoList" 
                          :key="item" 
                          :value="item" 
                          :label="item"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="区域" prop="from.startRegionCode">
                      <el-select v-model="ticketForm.from.startRegionCode" style="width: 100%" size="mini" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in regionList" 
                          :key="item.dictValue" 
                          :value="item.dictValue" 
                          :label="item.dictLabel"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-if="source == '2'">
                <el-row>
                  <li>车辆信息</li>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="车号">
                      <el-input v-model="ticketForm.from.carNo" size="mini" :disabled="isShowAmount" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="区域" prop="from.startRegionCode">
                      <el-select v-model="ticketForm.from.startRegionCode" size="mini" style="width:100%" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in regionList" 
                          :key="item.dictValue" 
                          :value="item.dictValue" 
                          :label="item.dictLabel"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-if="source == '3'">
                <el-row>
                  <li>场地信息</li>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="库场" prop="from.storehouseId">
                      <el-select v-model="ticketForm.from.storehouseId" size="mini" @change="changeStorehouse(ticketForm.from.storehouseId,'from')" style="width:100%" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in storehouseList" 
                          :key="item.id" 
                          :value="item.id" 
                          :label="item.storehouseName"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="货位">
                      <el-select v-model="ticketForm.from.locationId" size="mini" style="width:100%" @change="changeLocation(ticketForm.from.locationId,'from')" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in cargoSpaceList" 
                          :key="item.locationId" 
                          :value="item.locationId" 
                          :label="item.locationNo"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="垛号" prop="stackPosition">
                      <el-input v-model="ticketForm.from.stackPosition" size="mini" clearable :disabled="isShowAmount"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="区域" prop="from.startRegionCode">
                      <el-select v-model="ticketForm.from.startRegionCode" size="mini" style="width:100%" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in regionList" 
                          :key="item.dictValue" 
                          :value="item.dictValue" 
                          :label="item.dictLabel"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>

              <!-- 到区域 --
              <div v-if="destination == '1'">
                <el-row style="margin-top: 10px;">
                  <li>船舶信息</li>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="船舶航次" prop="to.nameVoyage">
                      <el-input v-model="ticketForm.to.nameVoyage" size="mini" readonly :disabled="isShowAmount"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="泊位" prop="berthNo">
                      <el-input v-model="ticketForm.to.berthNo" size="mini" :disabled="isShowAmount" readonly></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="舱口" prop="to.cabinNo">
                      <el-select v-model="ticketForm.to.cabinNo" :disabled="isShowAmount" style="width:100%;" size="mini" multiple>
                        <el-option 
                          v-for="item in cabinNoList" 
                          :key="item" 
                          :value="item" 
                          :label="item"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="区域" prop="endRegionCode">
                      <el-select v-model="ticketForm.to.endRegionCode" style="width: 100%" size="mini" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in regionList" 
                          :key="item.dictValue" 
                          :value="item.dictValue" 
                          :label="item.dictLabel"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-if="destination == '2'">
                <el-row style="margin-top: 10px;">
                  <li>车辆信息</li>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="车号">
                      <el-input v-model="ticketForm.to.carNo" size="mini" clearable :disabled="isShowAmount"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="区域" prop="to.endRegionCode">
                      <el-select v-model="ticketForm.to.endRegionCode" style="width:100%" size="mini" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in regionList" 
                          :key="item.dictValue" 
                          :value="item.dictValue" 
                          :label="item.dictLabel"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-if="destination == '3'">
                <el-row style="margin-top: 10px;">
                  <li>场地信息</li>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="库场" prop="to.storehouseId">
                      <el-select v-model="ticketForm.to.storehouseId" size="mini" @change="changeStorehouse(ticketForm.to.storehouseId,'to')" style="width:100%" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in storehouseList" 
                          :key="item.id" 
                          :value="item.id" 
                          :label="item.storehouseName"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="货位">
                      <el-select v-model="ticketForm.to.locationId" size="mini" style="width:100%" @change="changeLocation(ticketForm.to.locationId,'to')" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in cargoSpaceList" 
                          :key="item.locationId" 
                          :value="item.locationId" 
                          :label="item.locationNo"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="垛号" prop="stackPosition">
                      <el-input v-model="ticketForm.to.stackPosition" size="mini" clearable :disabled="isShowAmount"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="区域" prop="to.endRegionCode">
                      <el-select v-model="ticketForm.to.endRegionCode" style="width:100%" size="mini" :disabled="isShowAmount">
                        <el-option 
                          v-for="item in regionList" 
                          :key="item.dictValue" 
                          :value="item.dictValue" 
                          :label="item.dictLabel"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <el-row style="margin-top: 10px;">
                <li>作业信息</li>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="车数">
                    <el-input v-model="ticketForm.carNum" size="mini" clearable :disabled="isShowAmount"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="件数">
                    <el-input v-model="ticketForm.quantity" size="mini" clearable :disabled="isShowAmount"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="重量">
                    <template slot="label">
                      <span>重量</span>
                      <el-button style="margin-left: 10px;" type="text" size="mini" @click="useWeightData" :disabled="isShowAmount">使用理货小票数据</el-button>
                    </template>
                    <el-input v-model="ticketForm.ton" size="mini" clearable :disabled="isShowAmount"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="体积">
                    <el-input v-model="ticketForm.volume" size="mini" clearable :disabled="isShowAmount"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="货物形态">
                    <el-select v-model="ticketForm.cargoFormCode" size="mini" clearable style="width:100%" @change="changeCargoForm(ticketForm.cargoFormCode,'1')" :disabled="isShowAmount">
                      <el-option
                        v-for="(item,index) in cargoForms"
                        :key="index"
                        :value="item.dictValue"
                        :label="item.dictLabel"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="变化后形态">
                    <el-select v-model="ticketForm.changeFormCode" size="mini" clearable style="width:100%" @change="changeCargoForm(ticketForm.changeFormCode,'2')" :disabled="isShowAmount">
                      <el-option
                        v-for="(item,index) in cargoForms"
                        :key="index"
                        :value="item.dictValue"
                        :label="item.dictLabel"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="16">
                  <el-form-item label="备注">
                    <el-input type="textarea" clearable v-model="ticketForm.remark" :disabled="isShowAmount"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form> -->
            <!-- 分配作业量 -->
            <tallyAmount ref="amountRef" v-if="isShowAmount" :initTicketInfo="initTicketInfo" :ticketForm="ticketForm" :workLineInfo="workLineInfo" :selectMachineClick="selectMachineClick" :processList="processList"></tallyAmount>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
    <div class="demo-drawer__footer">
      <el-button type="primary" size="mini" @click="addUpdate" v-if="!isShowAmount">保存</el-button>
      <el-button type="primary" size="mini" @click="insertAmount" v-else>保存</el-button>
    </div>
  </div>
</template>

<script>
import BaseTable from '@/components/BaseTable'
import tallyAmount from './tallyAmount'
import publicapi from '@/api/public/index'
import api from '@/api/yardTally/singTicket/index'
export default {
  components: { BaseTable,tallyAmount },
  props: {
    workLineInfo: {
      type: Object,
    },
    isShowAmount: {
      type: Boolean,
      default: false,
    }
  },
  data () {
    return {
      activeName: '',
      cargoInfoList: [],
      ticketId: '', // 选中的子过程按钮
      selectTicketId: '', // 作业票id
      processList: [], // 子过程List
      subProcessCode: '', // 作业过程
      source: '', // 从区域
      destination: '', // 到区域
      // 表单内容
      ticketForm: {
        carNum: '',
        quantity: '',
        ton: '',
        remark: '',
        cargoFormCode: '',
        changeFormCode: '',
        cargoInfoId: '',
        processCode: '',
        from: {
          nameVoyage: '',
          carNo: '',
        },
        to: {
          nameVoyage: '',
          carNo: '',
        },
      },
      // 表单校验
      ruleForm: {
        'from.nameVoyage': [
          { required: true, message: '请选择船名航次', trigger: 'change' }
        ],
        'to.nameVoyage': [
          { required: true, message: '请选择船名航次', trigger: 'change' }
        ],
        'from.locationId': [
          { required: true, message: '请选择货位', trigger: 'change' }
        ],
        'to.locationId': [
          { required: true, message: '请选择货位', trigger: 'change' }
        ],
        'from.cabinNo': [
          { required: true, message: '请选择舱口', trigger: 'blur' }
        ],
        'to.cabinNo': [
          { required: true, message: '请选择舱口', trigger: 'blur' }
        ],
        'from.storehouseId': [
          { required: true, message: '请选择场地', trigger: 'change' }
        ],
        'to.storehouseId': [
          { required: true, message: '请选择场地', trigger: 'change' }
        ],
        'from.startRegionCode': [
          { required: true, message: '请选择区域', trigger: 'change,blur' }
        ],
        'to.endRegionCode': [
          { required: true, message: '请选择区域', trigger: 'change,blur' }
        ],
      },
      // 舱口
      cabinNoList: ['1','2','3','4','5','6','7','8'],
      regionList: [], // 区域
      storehouseList: [], // 库场
      cargoForms: [], // 货物形态
      cargoSpaceList: [], // 货位
      processDisable: false,
      selectData: [],
    }
  },
  created () {
    this.getDictData()
    this.initTicket()
    this.getStorehouse()
  },
  methods: {
    // 查询字典信息
    getDictData () {
      publicapi.getDictData('REGION,CARGO_FORM').then(res => {
        this.regionList = res.data.data.REGION
        this.cargoForms = res.data.data.CARGO_FORM
      })
    },
    // 查询库场信息
    getStorehouse () {
      publicapi.listStorehouse().then(res => {
        this.storehouseList = res.data.data
      })
    },
    // 初始化票货信息
    initTicket (flag) {
      let params = {
        id: this.workLineInfo.id,
        processCode: this.workLineInfo.processCode
      }
      api.queryTYardTicket(params).then(res => {
        let data = res.data.data
        this.cargoInfoList = data.ticketCargoSignList
        this.processList = data.processList

        if (flag == 1) {
          this.collapseChanges(this.activeName)
        }
      })
    },
    // 点击折叠框
    collapseChanges (v) {
      console.log(v)
      if (this.activeName === '') {
        return
      }
      if (this.$refs.ticketInfo)
        this.$refs.ticketInfo[0].clearValidate()
      this.restForm('1')
      let cargoList = this.cargoInfoList.filter((v,i) => i == this.activeName)[0]
      console.log(cargoList)
      
      if (cargoList.tallyList && cargoList.tallyList.length > 0) {
        // 如果保存了作业票 过程list不为空 默认选中第一个
        this.ticketId = cargoList.tallyList[0].id
        this.selectTicketId = cargoList.tallyList[0].cargoInfoId
        this.subProcessCode = cargoList.tallyList[0].processCode
        this.changeTiketInfo(this.ticketId)
      } else {
        this.subProcessCode = this.processList[0].processCode
        this.ticketForm.cargoInfoId = cargoList.cargoInfoId
        this.ticketForm.processCode = this.subProcessCode
      }
      
      // 通过选中的子过程改变页面显示
      this.changeSourceDestination(this.subProcessCode)

    },
    // 选中子过程
    changeTiketInfo (val) {
      this.restForm('1')
      console.log(val)
      let cargoInfo = this.cargoInfoList.filter((v,i) => i == this.activeName)[0]
      let id = cargoInfo.tallyList[0].cargoInfoId
      let processCode = cargoInfo.tallyList.filter(v => v.id == val)[0].processCode
      let params = {
        planId: this.workLineInfo.id,
        cargoInfoId: id,
        processCode: processCode
      }
      this.processDisable = true

      this.subProcessCode = processCode
      // 通过选中的子过程改变页面显示
      this.changeSourceDestination(processCode)

      api.queryById(params).then(res => {
        let data = res.data.data ? res.data.data[0] : null
        console.log(data)
        if (data != null) {
          this.ticketForm = JSON.parse(JSON.stringify(data))
          // this.ticketForm.from = data.from[0]
          // this.ticketForm.to = data.to[0]
          //从
          // switch (this.source) {
          //   case '1': 
          //     this.ticketForm.from.nameVoyage = this.workLineInfo.nameVoyage
          //     this.ticketForm.from.cabinNo = this.ticketForm.from.cabinNo.split(',')
          //     this.ticketForm.from.berthNo = this.workLineInfo.berthNo
          //   case '2': 
          //     break
          //   case '3': 
          //     this.changeStorehouse(this.ticketForm.from.storehouseId)
          //   default:
          //     break;
          // }
          // //到
          // switch (this.destination) {
          //   case '1': 
          //     this.ticketForm.to.nameVoyage = this.workLineInfo.nameVoyage
          //     this.ticketForm.to.cabinNo = this.ticketForm.to.cabinNo.split(',')
          //     this.ticketForm.to.berthNo = this.workLineInfo.berthNo
          //   case '2': 
          //     break
          //   case '3': 
          //     this.changeStorehouse(this.ticketForm.to.storehouseId)
          //   default:
          //     break;
          // }
          if (this.$refs.ticketInfo)
            this.$refs.ticketInfo[0].clearValidate()
        } else {
          this.processDisable = false
        }
      })
    },
    // 通过子过程改变页面显示的从/到信息
    changeSourceDestination (val) {
      // this.restForm()
      let oprocess = this.processList.filter(i => i.processCode == val)
      console.log(oprocess)
      this.source = oprocess[0].source
      this.destination = oprocess[0].destination

      this.ticketForm.processCode = val
      this.ticketForm.processName = this.processList.filter(i => i.processCode == val)[0].processName
      
      // 对不同显示的信息赋初始值
      //船舶
      if (this.source == '1') {
        this.ticketForm.from.nameVoyage = this.workLineInfo.nameVoyage
        this.ticketForm.from.shipvoyageId = this.workLineInfo.shipvoyageId
        this.ticketForm.from.berthNo = this.workLineInfo.berthNo
        this.ticketForm.from.cabinNo = this.workLineInfo.cabinNo?this.workLineInfo.cabinNo.split(','):[]
      }
      if (this.destination == '1') {
        this.ticketForm.to.nameVoyage = this.workLineInfo.nameVoyage
        this.ticketForm.to.shipvoyageId = this.workLineInfo.shipvoyageId
        this.ticketForm.from.berthNo = this.workLineInfo.berthNo
        this.ticketForm.to.cabinNo = this.workLineInfo.cabinNo?this.workLineInfo.cabinNo.split(','):[]
      }
      
      if (this.$refs.ticketInfo)
        this.$refs.ticketInfo[0].clearValidate()
    },
    // 通过库场查货位/区域
    changeStorehouse (val,item) {
      if (!val)
        return
      if (item == 'from') {
        this.ticketForm[item]['storehouseName'] = this.storehouseList.filter(v => v.id == val)[0].storehouseName
        this.$set(this.ticketForm.from,'locationId','')
        this.$set(this.ticketForm.from,'locationNo','')
      } else if (item == 'to') {
        this.ticketForm[item]['storehouseName'] = this.storehouseList.filter(v => v.id == val)[0].storehouseName
        this.$set(this.ticketForm.to,'locationId','')
        this.$set(this.ticketForm.to,'locationNo','')
      }
      this.cargoSpaceList = []
      // 查询对应货位
      publicapi.getStorehouseLocation(val).then(res => {
        this.cargoSpaceList = res.data.data
      })
      // 通过库场id查对应区域
      api.listRegionByStorehouseId(val).then(res => {
        let data = res.data.data
        if (item == 'from') {
          this.$set(this.ticketForm.from,'startRegionCode',data.regionCode)
          this.$set(this.ticketForm.from,'startRegionName',data.regionName)
        }
        if (item == 'to') {
          this.$set(this.ticketForm.to,'endRegionCode',data.regionCode)
          this.$set(this.ticketForm.to,'endRegionName',data.regionName)
        }
      })
    },
    // 选择货物形态
    changeCargoForm (val,flag) {
      if (!val)
        return
      if (flag == '1') {
        this.ticketForm.cargoFormName = this.cargoForms.filter(v => v.dictValue == this.ticketForm.cargoFormCode)[0].dictLabel
      } else {
        this.ticketForm.changeFormCode = this.cargoForms.filter(v => v.dictValue == this.ticketForm.changeFormCode)[0].dictLabel
      }
      
    },
    // 选择货位
    changeLocation (val,item) {
      if (!val)
        return
      if (item == 'from') {
        this.ticketForm.from.locationNo = this.cargoSpaceList.filter(v => v.locationId == val)[0].locationNo
      } else if (item == 'to') {
        this.ticketForm.to.locationNo = this.cargoSpaceList.filter(v => v.locationId == val)[0].locationNo
      }
    },
    // 新增编辑作业票
    addUpdate () {
      if (this.ticketForm.id) {
        this.updateTicket()
      } else {
        this.saveTicket()
      }
    },
    // 新增作业票
    saveTicket () {
      this.$refs.ticketInfo[0].validate((valid) => {
        if (!valid) {
          return false
        }
        let params = this.ticketForm
        params.source = this.source
        params.destination = this.destination
        let cargoInfo = this.cargoInfoList.filter((v,i) => i == this.activeName)[0]
        
        if (params.from.cabinNo && params.from.cabinNo.length < 1) {
          params.from.cabinNo = params.from.cabinNo.join(',')
        }
        if (params.to.cabinNo && params.to.cabinNo.length < 1) {
          params.to.cabinNo = params.to.cabinNo.join(',')
        }
        // 从的区域
        params.from.startRegionName = this.regionList.filter(v => v.dictValue == params.from. startRegionCode)[0].dictLabel

        switch (this.source) {
          case '1':
            params.from.cabinNo = params.from.cabinNo.join(',')
          default:
            break
        }

        // 到的区域
        params.to.endRegionName = this.regionList.filter(v => v.dictValue == params.to.endRegionCode) [0].dictLabel

        switch (this.destination) {
          case '1':
            params.to.cabinNo = params.to.cabinNo.join(',')
          default:
            break
        }
        params.planId = this.workLineInfo.id
        params.workDate = this.workLineInfo.workDate
        params.workTime = this.workLineInfo.workTime
        params.cargoInfoId = cargoInfo.cargoInfoId
        params.processCode = this.subProcessCode
        params.processName = this.processList.filter(v => v.processCode == this.subProcessCode)[0]. processName
        params.cargoCode = cargoInfo.cargoCode
        params.cargoName = cargoInfo.cargoName
        params.packing = cargoInfo.packing

        let from = params.from
        params.from = [from]
        let to = params.to
        params.to = [to]


        api.addTYardTicket(params).then(res => {
          if (res.data.code != '0000') {
            return this.$message.error(res.data.msg)
          }
          // 刷新新增的子过程
          this.initTicket(1)
          this.$message.success('保存成功')
        })
      })
      
    },
    // 编辑作业票
    updateTicket () {
      this.$refs.ticketInfo[0].validate((valid) => {
        if (!valid) {
          return false
        }
        let params = this.ticketForm
        params.source = this.source
        params.destination = this.destination
        console.log(params)
        switch (this.source) {
          case '1':
            params.from.cabinNo = params.from.cabinNo.join(',')
          default:
            break
        }
        switch (this.destination) {
          case '1':
            params.to.cabinNo = params.to.cabinNo.join(',')
          default:
            break
        }
        let from = params.from
        let to = params.to
        params.from = [from]
        params.to = [to]
        api.updateTYardTicket(params).then(res => {
          if (res.data.code != '0000') {
            return this.$message.error(res.data.msg)
          }
          this.activeName = ''
          this.$message.success('更新成功')
        })
      })
    },
    // 新增子过程
    addTicket () {
      this.restForm()
      this.processDisable = false
      this.$refs.amountRef[0].insertTicket()
    },
    // 清空表单
    restForm (flag) {
      if (flag != '1') {
        this.ticketId = ''
        this.selectTicketId = ''
      }
      let from = {
        nameVoyage: this.workLineInfo.nameVoyage,
        cabinNo: [],
        berthNo: this.workLineInfo.berthNo,
        startRegionCode: '',
        startRegionName: '',
        storehouseId: '',
        storehouseName: '',
        locationId: '',
        locationNo: '',
        carNo: '',
        stackPosition: '',
      }
      let to = {
        nameVoyage: this.workLineInfo.nameVoyage,
        cabinNo: [],
        berthNo: this.workLineInfo.berthNo,
        startRegionCode: '',
        startRegionName: '',
        storehouseId: '',
        storehouseName: '',
        locationId: '',
        locationNo: '',
        carNo: '',
        stackPosition: '',
      }
      this.$set(this.ticketForm,'from',from)
      this.$set(this.ticketForm,'to',to)
      this.$set(this.ticketForm,'carNum','')
      this.$set(this.ticketForm,'quantity','')
      this.$set(this.ticketForm,'ton','')
      this.$set(this.ticketForm,'volume','')
      this.$set(this.ticketForm,'cargoFormCode','')
      this.$set(this.ticketForm,'cargoFormName','')
      this.$set(this.ticketForm,'changeFormCode','')
      this.$set(this.ticketForm,'changeFormName','')
      this.$set(this.ticketForm,'remark','')
      this.$set(this.ticketForm,'id','')
      this.cargoSpaceList = []
    },
    // 删除作业票
    deleteTicket () {
      let params = {
        id: this.ticketId,
        planId: this.workLineInfo.id
      }
      api.deleteById(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.$message.success(res.data.msg)
        this.$refs.amountRef[0].insertTicket()
        this.initTicket()
        this.collapseChanges(this.activeName)
      })
    },
    // 使用理货小票数据
    useWeightData () {
      let cargoInfo = this.cargoInfoList.filter((v,i) => i == this.activeName)[0]
      let params = {
        processCode: this.subProcessCode,
        planId: this.workLineInfo.id,
        cargoInfoId: cargoInfo.cargoInfoId
      }
      api.selectYardTally(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        let data = res.data.data[0]
        this.ticketForm.carNum = data.carSum
        this.ticketForm.quantity = data.QuatitySum
        this.ticketForm.ton = data.tonSum
        this.ticketForm.volume = data.volumeSum
      })
    },
    // 选中的机械
    selectMachineClick (val) {
      this.selectData = val
    },
    // 保存作业量
    insertAmount () {
      this.$refs.amountRef[this.activeName].submitAmount()
    },
    // 使用理货数据
    useTallyData () {
      this.$refs.amountRef[0].useTallyData()
    },
    // 签票成功后回调
    initTicketInfo (flag) {
      this.initTicket(flag)
    },
  }
}
</script>

<style lang="less" scoped>
@import "../index.less";
@import "../../../../assets/searchfrom.less";
.buttonclass {
  float: right;
}
</style>